package com.chisheng.caa.controller;

import com.chisheng.caa.pojo.response.JsonResponse;
import com.chisheng.caa.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import static com.chisheng.caa.service.FileService.UPLOAD_FORM_PARAM_NAME;

@CrossOrigin
@Api("文件操作控制器")
@RestController
@RequestMapping("/api/caa/file")
public class FileController {

    @Resource
    private FileService fileService;

    @ApiOperation("文件上传")
    @PostMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResponse<String> upload(@ApiParam("上传的文件") @RequestParam(UPLOAD_FORM_PARAM_NAME) MultipartFile file) {
        return JsonResponse.success(fileService.upload(file));
    }

    @ApiOperation("文件下载")
    @GetMapping("/download/{fileCode}")
    public void download(HttpServletResponse response, @ApiParam("文件码") @PathVariable String fileCode) {
        fileService.output(response, fileCode);
    }

}
